//
//  BookActivity.swift
//  SwiftDemo
//
//  Created by Jason on 15/11/9.
//  Copyright © 2015年 Jason Jiang. All rights reserved.
//

public typealias activityBlock = () ->Void

import UIKit

class BookActivity: UIActivity {
    
    var text:String!
    var url:NSURL!
    var image:UIImage!
    var block :activityBlock = {
        
    }
    
    override func activityType() -> String? {
        
        return BookActivity.self.description()
    }
    
    override func activityTitle() -> String? {
        
        return "title"
    }
    
    override func activityImage() -> UIImage? {
        
        return UIImage (named: "1.png")
    }
    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        
        NSLog("can perform");
        return true
    }
    

    
    override class func activityCategory() ->UIActivityCategory{
        
        return UIActivityCategory.Share;
        
    }
    
    //解析分享数据时调用，可以进行一定的处理
    override func prepareWithActivityItems(activityItems: [AnyObject]) {
        NSLog("prepareWithActivityItems")
        for item in activityItems {
            if item is UIImage {
                image = item as! UIImage
            }
            if item is String {
                text = item as! String
            }
            if item is NSURL {
                url = item as! NSURL
            }
        }
    }
    
    
    //执行分享行为
    //这里根据自己的应用做相应的处理
    //例如你可以分享到另外的app例如微信分享，也可以保存数据到照片或其他地方，甚至分享到网络
    override func performActivity() {
        NSLog("performActivity")
        //具体的执行代码这边先省略
        self.activityDidFinish(true)
        block();
        
    }
    
    //分享时调用
    override func activityViewController() -> UIViewController? {
        NSLog("activityViewController")
  
        return nil
    }
    
    override func activityDidFinish(completed: Bool) {
        
//        if (completed){
        
            NSLog("can share the content")
//        }
    }

}
